//
//  YZMHomeBannerModel.m
//  YZMicroStock
//
//  Created by Lv.Dongdong on 16/10/13.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMHomeBannerModel.h"
#import "YZMWebViewService.h"
#import "YZMWebViewController.h"

#define sYZMBannerListDataDefault @"YZMAdDataDefault"

@implementation YZMHomeBannerModel

+ (NSArray *)mapResponseArray:(NSArray *)responseArray type:(NSInteger)type {
    
    NSMutableArray *resultArr = [NSMutableArray array];
    if (responseArray.count > 0) {
        
        for (NSArray *array in responseArray) {
            
            if ([array[2] integerValue] == type) {
                
                NSMutableDictionary *resultDic = [NSMutableDictionary dictionary];
                //唯一ID
                resultDic[@"ID"] = array[0];
                //标题
                resultDic[@"title" ] = array[1];
                //imageUrl转换utf8
                if ([array[3] hasPrefix:@"http"]) {
                    
                    resultDic[@"imageUrl"] = array[3]; //image下载URL
                } else {
                    
                    NSString *imageUrlStr = [NSString stringWithFormat:@"%@%@", URL_BASE_STRING, array[3]];
                    resultDic[@"imageUrl"] = imageUrlStr; //image下载URL
                }

                if (array.count == 4) {
                    
                    resultDic[@"bannerEvent"] = @{@"event":@"", @"url":@""};
                } else if (array.count == 6) {
                    
                    resultDic[@"bannerEvent"] = @{@"event":array[4], @"url":array[5]};//点击banner的事件或URL
                    
                } else if (array.count == 9) {
                    
                    resultDic[@"bannerEvent"] = @{@"event":array[4], @"url":array[5]};//点击banner的事件或URL
                    resultDic[@"urlEvent"] = @{@"btnName":array[6], @"event":array[7], @"url":array[8]};//点击按钮的事件或URL
                } else {
                    
                    return nil;
                }
                
                //解析后的数据
                [resultArr addObject:resultDic];
            }
        }
        
        if (resultArr.count > 0) {
            
            return resultArr;
        }
    }
    
    return nil;
}

+ (NSDictionary *)eventResult {
    
    return @{@"yztz://ycl.yztz.com/recharge":@{@"login":@(YES), @"page":@"recharge"},
             @"yztz://ycl.yztz.com/trade":@{@"page":@"stock"},
             @"yztz://ycl.yztz.com/tdtrade":@{@"page":@"stockTD"},
             //@"yztz://ycl.yztz.com/simtrade":@{@"login":@(YES), @"page":@"Simulate"},
             @"yztz://ycl.yztz.com/extension":@{@"page":@"AboutSpread"},
             @"yztz://ycl.yztz.com/realcert":@{@"login":@(YES), @"page":@"identifyVerify"},
             @"yztz://ycl.yztz.com/payZFcert":@{@"login":@(YES), @"page":@"payZFAuth"},
             @"yztz://ycl.yztz.com/greenhand":@{@"login":@(YES), @"page":@"RedPacketAct"},
             @"yztz://ycl.yztz.com/actTrade":@{@"login":@(YES),@"page":@"freeExperience"},
//             @"yztz://ycl.yztz.com/acttrade":@{@"login":@(YES), @"page":@"oneYuan"},
             @"yztz://ycl.yztz.com/register":@{@"page":@"register"},
             
             };
}

+ (void)selectDirection:(NSDictionary *)dic withLocal:(BOOL)isLocal {
    
    if (!dic) return;
    
    NSString *event = nil;
    if (isLocal) {
        
        event = dic[@"bannerEvent"][@"event"];
    } else {
        
        event = dic[@"event"];
    }
    
    if ([event isEqualToString:@""]) {
        
        if ([[YZMWebViewService sharedService] containUrlStr:dic[@"url"]]) {
            
            return;
        }
        
        if (isLocal) {
            
            //跳转H5页面
            [YZMWebViewService openBannerWithDic:dic];
        } else {
            
            //H5跳转H5页面
            YZMWebViewController *webVC = (YZMWebViewController *)[YZMCMInstance instanceWithControllerName:@"webView"];
            [webVC urlProcess:dic[@"url"]];
        }
    } else {
        
        [YZMHomeBannerModel tradeParam:event withDic:dic[@"param"]];
    }
}

+ (BOOL)tradeParam:(NSString *)event withDic:(NSDictionary *)dic {
    
    NSDictionary *eventDic = [YZMHomeBannerModel eventResult];
    NSDictionary *paramDic = [eventDic objectForKey:[event lowercaseString]];
    
    if (paramDic) {
        
        NSMutableDictionary *param = [NSMutableDictionary dictionary];
        if (paramDic[@"params"]) {
            
            [param addEntriesFromDictionary:paramDic[@"params"]];
            if (dic) [param addEntriesFromDictionary:dic];
        } else {
            
            if (dic) [param addEntriesFromDictionary:dic];
        }
        
        if ([paramDic[@"login"] boolValue]) {
            
            //需要登录后跳转
            [YZMCMInstance pushByCheckingAuthWithName:paramDic[@"page"] andParams:param];
        } else {
            
            //跳转本地页面
            [YZMCMInstance pushViewControllerWithName:paramDic[@"page"] andParams:param];
        }
        
        return YES;
    } else {
        
        NSURL *url = [NSURL URLWithString:event];
        NSString *urlStr = [NSString stringWithFormat:@"%@://%@%@", url.scheme, url.host, url.path];
        paramDic = [eventDic objectForKey:[urlStr lowercaseString]];
        
        if (paramDic) {
            
            NSMutableDictionary *mutableDic = [NSMutableDictionary dictionary];
            for (NSString *string in [url.query componentsSeparatedByString:@"&"]) {
                
                NSArray *array = [string componentsSeparatedByString:@"="];
                if (array.count != 2) {
                    continue;
                }
                
                [mutableDic setObject:[array[1] lowercaseString] forKey:[array[0] lowercaseString]];
            }
            
            if (mutableDic.count > 0) {
                
                //跳转相应本地页面
                [YZMCMInstance pushViewControllerWithName:paramDic[@"page"] andParams:mutableDic];
                
                return YES;
            }
        }
    }
    
    return NO;
}

+ (void)createEncoder:(NSArray *)array {
    
    if (!array || array.count == 0) {
        
        return;
    }
    
    NSMutableArray *archiverArray = [NSMutableArray array];
    
    for (YZMHomeBannerModel *instance in array) {
        
        NSMutableData *data = [NSMutableData data];
        //创建一个归档类
        NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
        [archiver encodeObject:instance forKey:sYZMBannerListDataDefault];
        [archiver finishEncoding];
        
        [archiverArray addObject:data];
    }
    
    //将数据写入文件里
    [[NSUserDefaults standardUserDefaults] setObject:archiverArray forKey:sYZMBannerListDataDefault];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (NSArray *)createDecoder {
    
    NSArray *array = [[NSUserDefaults standardUserDefaults] objectForKey:sYZMBannerListDataDefault];
    
    if (!array ||array.count == 0) {
        
        return nil;
    }
    
    NSMutableArray *tempArray = [NSMutableArray array];
    
    for (NSData *data in array) {
        
        if (data && data.length != 0) {
            
            NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
            
            YZMHomeBannerModel *instance = [unarchiver decodeObjectForKey:sYZMBannerListDataDefault];
            [unarchiver finishDecoding];
            
            [tempArray addObject:instance];
        }
    }
    
    if (tempArray.count == 0) {
        
        return nil;
    }
    
    return tempArray;
}

@end
